import pandas as pd

def singleton(class_):
    instances = {}
    def getinstance(*args, **kwargs):
        if class_ not in instances:
            instances[class_] = class_(*args, **kwargs)
        return instances[class_]
    return getinstance


def join(raw):
    if raw is None:
        return []
    else:
        return raw


def parse_fields(result, fields):
    export_results = []
    for one in result:
        export_result = {}
        for field in fields:
            value = get_fields(one, field)
            export_result[field] = value
        export_results.append(export_result)
    export_results = pd.DataFrame.from_dict(export_results)
    return export_results


def get_fields(obj:object, flat_field:str) -> str:
    """从单个obj中提取fields内容
    
    
    
    """
    fields = flat_field.split('.')

    try:
        current_obj = obj
        for field in fields:
            current_obj = current_obj[field] if current_obj[field] else ''

        if isinstance(current_obj, int):
            current_obj = str(current_obj)

        if isinstance(current_obj, list):
            current_obj = ','.join([str(i) for i in current_obj])

        return current_obj

    except Exception as e:
        return ''